package com.clgc.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.clgc.eduservice.config.security.JwtTokenUtils;
import com.clgc.eduservice.entity.EduStudent;
import com.clgc.eduservice.entity.EduTeacher;
import com.clgc.eduservice.mapper.EduStudentMapper;
import com.clgc.eduservice.service.EduStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clgc.utils.MD5Utils;
import com.clgc.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 学生表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-04-27
 */
@Service
public class EduStudentServiceImpl extends ServiceImpl<EduStudentMapper, EduStudent> implements EduStudentService {

    @Qualifier("myUserDetailsService")
    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private JwtTokenUtils jwtTokenUtils;

    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @Override
    public R login(String username, String pwd, HttpServletRequest request) {
        System.out.println("---------------------login---------------------");
        EduStudent userDetails = (EduStudent) userDetailsService.loadUserByUsername(username);
        //检验用户是否存在，密码是否正确，是否逻辑删除为1
        System.out.println("userDetails-------->" + userDetails);
        System.out.println("MD5Utils.MD5Encode(pwd).equals(userDetails.getPassword())-------->"+MD5Utils.MD5Encode(pwd).equals(userDetails.getPassword()));
        if (null == userDetails || !MD5Utils.MD5Encode(pwd).equals(userDetails.getPwd()) || userDetails.getIsDelete() == 1) {
            return R.error().message("用户名或密码错误");
        }

        //设置全局用户对象
        UsernamePasswordAuthenticationToken authenticationToken
                = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        //生成token
        System.out.println("----------生成token------------");

        System.out.println("userDetails----->"+userDetails);
        System.out.println("userDetails.getUsername()---->"+userDetails.getUsername());
        String token = jwtTokenUtils.generateToken(userDetails);
        String userNameFromToken = jwtTokenUtils.getUserNameFromToken(token);
        System.out.println(userNameFromToken);
        System.out.println("----------生成token------------");
        Map<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        System.out.println("------------------");
        System.out.println(SecurityContextHolder.getContext().getAuthentication());
        System.out.println("------------------");
        return R.ok().message("登陆成功").data(tokenMap);
    }

    @Override
    public EduStudent getStudentByUsername(String username) {
        QueryWrapper<EduStudent> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        return baseMapper.selectOne(wrapper);
    }
}
